using System;
using System.Globalization;
using System.IO;
using System.Threading;
using JetBrains.Annotations;
using SF.Code.Extension.CollectionsHelper;
using SF.Code.Extension.EnumHelper;
using SF.Code.Extension.NLP;

namespace SG.Productivity.DesktopControlPanel.Support
{
    public enum ProjectType
    {
        Angebot = 0,
        Auftrag = 1,
        Reklamation = 2,

        Adresse = 1000,

    }

    public static class ProjectTypeEx
    {
        private static readonly IReadOnlyArray<ProjectType> Types = new ReadOnlyArray<ProjectType>(EnumEx.GetDistinctEnumMembers<ProjectType>());

        [Pure]
        public static ProjectType? IndexToType(int index)
        {
            if (index < 0)
                return null;

            return Types[index];
        }

        [Pure]
        public static int ToIndex(this ProjectType @this)
        {
            for (int i = 0; i < Types.Count; i++)
            {
                if (Types[i] == @this)
                    return i;
            }

            return -1;
        }

        public static string PathPrefix(this ProjectType @this)
        {
            switch (@this)
            {
                default:
                    return Path.Combine("G:", "text", "Dokumente");
            }
        }

        public static Noun Singular(this ProjectType @this, CultureInfo language = null)
        {
            if (language == null)
                language = Thread.CurrentThread.CurrentUICulture;

            return new Noun(@this.ToString(), language, Quantity.Singular);
        }

        public static Noun Plural(this ProjectType @this)
        {
            return @this.Singular().ToPlural();
        }


        [Pure]
        public static int Factor10(this ProjectType @this)
        {
            return @this.HasYearComponent() ? (int) Math.Pow(10, @this.Factor()) : 1;
        }

        [Pure]
        public static int Factor(this ProjectType @this)
        {
            return @this == ProjectType.Angebot ? 5 : @this.HasYearComponent() ? 4 : 1;
        }

        [Pure]
        public static bool HasYearComponent(this ProjectType @this)
        {
            switch (@this)
            {
                case ProjectType.Auftrag:
                case ProjectType.Angebot:
                case ProjectType.Reklamation:
                    return true;

                default:
                    return false;
            }
        }
    }
}